package com.mengfh.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/********************************************************************************
 ** @author ：ZYJ
 ** @date ：2023/04/23
 ** @description ：自定义拦截器 拦截ip
 *********************************************************************************/
@Slf4j
public class WhiteListIntercept implements HandlerInterceptor {


    @Autowired
    private IpService ipService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ip = null;
        try {
            if (request == null) {
                return false;
            }
            ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "127.0.0.1".equals(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "127.0.0.1".equals(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "127.0.0.1".equals(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "127.0.0.1".equals(ip)) {
                ip = request.getHeader("X-Real-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "127.0.0.1".equals(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            ip = "";
        }
        log.info("机主的ip是" + ip);
        WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        if (cxt != null && cxt.getBean(IpService.class) != null && ipService == null) {
            ipService = cxt.getBean(IpService.class);
        }
        if (ipService.getIp(ip)) {
            return true;
        } else {
//            returnJson(response, JSON.toJSONString(new ToolsResultEntity(1002, "ip不存在", null)));
            return false;
        }

//        return true;
    }

    /********************************************************************************
     ** @author ：ZYJ
     ** @date ：2023/04/23
     ** @description ：设置请求拦截返回参数
     *********************************************************************************/
    private void returnJson(HttpServletResponse response, String result) {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(result);
        } catch (IOException e) {
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}